package com.itcast.googleplay.factory;

import java.util.HashMap;

import com.itcast.googleplay.fragment.AppFragment;
import com.itcast.googleplay.fragment.BaseFragment;
import com.itcast.googleplay.fragment.CategoryFragment;
import com.itcast.googleplay.fragment.GameFragment;
import com.itcast.googleplay.fragment.HomeFragment;
import com.itcast.googleplay.fragment.HotFragment;
import com.itcast.googleplay.fragment.RecommendFragment;
import com.itcast.googleplay.fragment.SubjectFragment;

import android.support.v4.app.Fragment;

public class FragmentFactory {
	private static HashMap<Integer, BaseFragment> hashMap = new HashMap<Integer, BaseFragment>();
	public static BaseFragment getFragment(int position) {
		//1.在传递进来了索引以后,判断此索引指向对象是否已经存在,如果存在,则复用,没有则创建.hashMap<key,value> 去重复
		BaseFragment fragment = null;
		fragment = hashMap.get(position);
		if(fragment!=null){
			return fragment;
		}else{
			switch (position) {
			case 0:
				fragment = new HomeFragment();
				break;
			case 1:
				fragment = new AppFragment();
				break;
			case 2:
				fragment = new GameFragment();
				break;
			case 3:
				fragment = new SubjectFragment();
				break;
			case 4:
				fragment = new RecommendFragment();
				break;
			case 5:
				fragment = new CategoryFragment();
				break;
			case 6:
				fragment = new HotFragment();
				break;
			}
			hashMap.put(position, fragment);
			return fragment;
		}
	}
	
}
